package com.hangxintong.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    @Bean
    public Docket memberDocket(Environment environment) {
        ParameterBuilder ticketPar = new ParameterBuilder();
        Profiles profiles = Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles);
        List<Parameter> pars = new ArrayList<Parameter>();
        ticketPar.name("token").description("user token").defaultValue("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkZXZpY2VOdW0iOiIxMjM0NTYiLCJhdWQiOiIxODI0MTE0NjgzMiJ9.32uBcQ_Xd4PPvKFA-Kv2KgE3y_3pzw8BDr_sB0kbmm4")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(ticketPar.build());
        return new Docket(DocumentationType.SWAGGER_2).enable(flag).apiInfo(buildApiInf()).select()
                .apis(RequestHandlerSelectors.basePackage("com.hangxintong.member.controller"))
                .paths(PathSelectors.any()).build().groupName("会员服务").pathMapping("/")
                .globalOperationParameters(pars);
    }

    @Bean
    public Docket messageDocket(Environment environment) {
        ParameterBuilder ticketPar = new ParameterBuilder();
        Profiles profiles = Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles);
        List<Parameter> pars = new ArrayList<Parameter>();
        ticketPar.name("token").description("user token").defaultValue("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkZXZpY2VOdW0iOiIxMjM0NTYiLCJhdWQiOiIxODI0MTE0NjgzMiJ9.32uBcQ_Xd4PPvKFA-Kv2KgE3y_3pzw8BDr_sB0kbmm4")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(ticketPar.build());
        return new Docket(DocumentationType.SWAGGER_2).enable(flag).apiInfo(buildApiInf()).select()
                .apis(RequestHandlerSelectors.basePackage("com.hangxintong.controller"))
                .paths(PathSelectors.any()).build().groupName("消息服务").pathMapping("/")
                .globalOperationParameters(pars);
    }


    @Bean
    public Docket activityDocket(Environment environment) {
        ParameterBuilder ticketPar = new ParameterBuilder();
        ParameterBuilder xID = new ParameterBuilder();
        Profiles profiles = Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles);
        List<Parameter> pars = new ArrayList<Parameter>();

        ticketPar.name("token").description("user token").defaultValue("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkZXZpY2VOdW0iOiIxMjM0NTYiLCJhdWQiOiIxODI0MTE0NjgzMiJ9.32uBcQ_Xd4PPvKFA-Kv2KgE3y_3pzw8BDr_sB0kbmm4")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        xID.name("xID").description("协会id").defaultValue("135")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(ticketPar.build());
        pars.add(xID.build());
        return new Docket(DocumentationType.SWAGGER_2).enable(flag).apiInfo(buildApiInf()).select()
                .apis(RequestHandlerSelectors.basePackage("com.hangxintong.activity.controller"))
                .paths(PathSelectors.any()).build().groupName("1活动服务").pathMapping("/")
                .globalOperationParameters(pars);
    }

    @Bean
    public Docket certificateDocket(Environment environment) {
        ParameterBuilder ticketPar = new ParameterBuilder();
        Profiles profiles = Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles);
        List<Parameter> pars = new ArrayList<Parameter>();
        ticketPar.name("token").description("user token").defaultValue("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkZXZpY2VOdW0iOiIxMjM0NTYiLCJhdWQiOiIxODI0MTE0NjgzMiJ9.32uBcQ_Xd4PPvKFA-Kv2KgE3y_3pzw8BDr_sB0kbmm4")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(ticketPar.build());
        return new Docket(DocumentationType.SWAGGER_2).enable(flag).apiInfo(buildApiInf()).select()
                .apis(RequestHandlerSelectors.basePackage("com.hangxintong.certificate.controller"))
                .paths(PathSelectors.any()).build().groupName("证书服务").pathMapping("/")
                .globalOperationParameters(pars);
    }

    @Bean
    public Docket processDocket(Environment environment) {
        ParameterBuilder ticketPar = new ParameterBuilder();
        ParameterBuilder xID = new ParameterBuilder();
        Profiles profiles = Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles);
        List<Parameter> pars = new ArrayList<Parameter>();
        ticketPar.name("token").description("user token").defaultValue("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkZXZpY2VOdW0iOiIxMjM0NTYiLCJhdWQiOiIxODI0MTE0NjgzMiJ9.32uBcQ_Xd4PPvKFA-Kv2KgE3y_3pzw8BDr_sB0kbmm4")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        xID.name("xID").description("协会id").defaultValue("135")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(xID.build());
        pars.add(ticketPar.build());
        return new Docket(DocumentationType.SWAGGER_2).enable(flag).apiInfo(buildApiInf()).select()
                .apis(RequestHandlerSelectors.basePackage("com.hangxintong.process.controller"))
                .paths(PathSelectors.any()).build().groupName("流程服务").pathMapping("/")
                .globalOperationParameters(pars);
    }

    private ApiInfo buildApiInf(){
        return new ApiInfoBuilder()
                .title("行信通")
                .description("springboot swagger2")
                .build();
    }
}
